package com.zq.controller;

import com.zq.entity.Blog;
import com.zq.entity.Tag;
import com.zq.entity.Type;
import com.zq.entity.User;
import com.zq.repository.BlogRepository;
import com.zq.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * @Author:任人渐疏
 * @Date:Created in 10:40 上午 2020/6/28
 * Update:true
 */
@RestController
@RequestMapping("blog")
public class BlogController {
    @Autowired
    private BlogRepository blogRepository;
    @Autowired
    private BlogService blogService;

    @GetMapping
    public  Object getAllListBlog(Blog blog,Integer page,Integer typeid,Integer tagid,Integer userid){
        return blogService.findAllList(blog,page,typeid,tagid,userid);
    }
    @PostMapping
    public  Object AddBlog(Blog blog,Integer typeid,Integer userid){
//        Blog blog1 = blogRepository.findById(blog.getBid()).get();
//        blog1.setBiaoji(blog.getBiaoji());
//        blog1.setFabu(blog.isFabu());
//        blog1.setCreatetime(blog.getCreatetime());
//        blog1.setCreatetime(blog.getCreatetime());
//        blog1.setFirsturl(blog.getFirsturl());
//        blog1.setIsbanquan(blog.isIsbanquan());
//        blog1.setIspinglun(blog.isIspinglun());
//        blog1.setLiuyancishu(blog.getLiuyancishu());
        Type type = new Type();
        if (!StringUtils.isEmpty(typeid)) {
            type.setTid(typeid);
            blog.setType(type);
        }

        User user = new User();
        if (!StringUtils.isEmpty(userid)) {
            user.setUid(userid);
            blog.setUser(user);
        }
        return blogService.addBlog(blog,typeid,userid);
    }
    @PutMapping
    public  Object updateBlog(Blog blog,Integer userid,Integer typeid){
        System.out.println("blog"+blog);
        Blog blog1 = blogRepository.findById(blog.getBid()).get();
        blog1.setBiaoji(blog.getBiaoji());
        blog1.setFabu(blog.isFabu());
        blog1.setCreatetime(blog.getCreatetime());
        blog1.setCreatetime(blog.getCreatetime());
        blog1.setFirsturl(blog.getFirsturl());
        blog1.setIsbanquan(blog.isIsbanquan());
        blog1.setIspinglun(blog.isIspinglun());
        blog1.setLiuyancishu(blog.getLiuyancishu());
        Type type = new Type();
        if (!StringUtils.isEmpty(typeid)) {
            type.setTid(typeid);
            blog1.setType(type);
        }
//        Tag tag = new Tag();
//        if (!StringUtils.isEmpty(blog.getTagList().get)) {
//            tag.setTagid(tagid);
//        }
        User user = new User();
        if (!StringUtils.isEmpty(userid)) {
            user.setUid(userid);
            blog1.setUser(user);
        }
        return blogService.updateBlog(blog1);
    }
    @DeleteMapping
    public  Object deleteBlog(String blogid){
        return  blogService.deleteBlog(blogid);
    }

    @GetMapping("findById")
    public  Object findById(Integer bid){
        return blogRepository.findByBlogId(bid);
    }
}
